package faf.web;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import faf.business.ReportManager;
import faf.business.SurveyManager;
import faf.entities.Account;
import faf.entities.Survey;

public class ListReportsController implements Controller {
	SurveyManager surveyManager;
	ReportManager reportManager;

	public ReportManager getReportManager() {
		return reportManager;
	}

	public void setReportManager(ReportManager reportManager) {
		this.reportManager = reportManager;
	}

	public SurveyManager getSurveyManager() {
		return surveyManager;
	}

	public void setSurveyManager(SurveyManager manager) {
		this.surveyManager = manager;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Survey> surveys_ = surveyManager.listSurveys();
		Account user = (Account) request.getSession().getAttribute("user");

		List<Map> surveys = new LinkedList<Map>();
		
		for (Survey survey : surveys_) {
			boolean canSeeGraphicalReport = reportManager.canSeeGraphicalReport(user, survey);
			boolean canSeeFullReport = reportManager.canSeeFullreport(user, survey);
			
			if(canSeeGraphicalReport == true || canSeeFullReport == true) {
				Map<String, Object> survey_map= new HashMap<String, Object>();
				
				survey_map.put("survey", survey);
				survey_map.put("canSeeFullReport", canSeeFullReport);
				survey_map.put("canSeeGraphicalReport", canSeeGraphicalReport);
				
				surveys.add(survey_map);
			}
		}

		map.put("surveys", surveys);
		
		return new ModelAndView("reports", map);
	}
}
